package zookeeper;

import java.io.IOException ;

import org.apache.zookeeper.CreateMode ;
import org.apache.zookeeper.KeeperException ;
import org.apache.zookeeper.WatchedEvent ;
import org.apache.zookeeper.Watcher ;
import org.apache.zookeeper.ZooDefs.Ids ;
import org.apache.zookeeper.ZooKeeper ;

/**
 * zookeeper基本操作示例
 * @author 80002165 @date 2017年4月27日 下午6:22:52
 */
public class Test {
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zk = new ZooKeeper("localhost:2181", 2000, new Watcher(){
            // 监控所有被触发的事件
            public void process(WatchedEvent event) { 
                System.out.println("已经触发了" + event.getType() + "事件！"); 
            } 
        }) ;
        
        //创建一个目录节点
        zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT) ;
        
        //创建一个子目录节点
        if(zk.exists("/testRootPath/testChildPathOne", true) != null){
            System.out.println("已经存在节点") ;
        }else
            zk.create("/testRootPath/testChildPathOne", "OPEN_ACL_UNSAFE".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT) ;
        
        //拿子节点
        System.out.println(zk.getChildren("/testRootPath",true));
        System.out.println(zk.getData("/testRootPath/testChildPathOne", true, null)) ;
        System.out.println(zk.getData("/testRootPath", true, null)) ;
        
        zk.close();
    }
}
